// pagesWorkOrder/workOrderMyIndex/workOrderMyIndex.js
import {
  WorkOrderMyIndexApi
} from '../../common/apis/workOrder'
import {
  SUB_PAGES
} from '../../config/router.config'
import {
  getElHeightRpx
} from '../../utils/dom'
import {
  queryParams
} from '../../utils/util'

const app = getApp()
const mixin_dicKey = require('../../common/mixins/dicKey')

const chargebackWorkOrder = {
  label: '退单',
  buttonTheme: 'light',
  fnName: 'chargebackWorkOrder',
  tag: 'chargeback',
}
const acceptOrderWorkOrder = {
  label: '接单',
  buttonTheme: 'primary',
  fnName: 'acceptOrderWorkOrder',
  tag: '',
}
const completeWorkOrder = {
  label: '完成工单',
  buttonTheme: 'light',
  fnName: 'completeWorkOrder',
  tag: 'complete',
}
const handleWorkOrder = {
  label: '处理',
  buttonTheme: 'primary',
  fnName: 'handleWorkOrder',
  tag: 'handle',

}
const revokeWorkOrder = {
  label: '撤销',
  buttonTheme: 'light',
  fnName: 'revokeWorkOrder',
  tag: '',

}
const evaluateWorkOrder = {
  label: '立即评价',
  buttonTheme: 'primary',
  fnName: 'evaluateWorkOrder',
  tag: '',

}
const terminationWorkOrder = {
  label: '终止',
  buttonTheme: 'light',
  fnName: 'terminationWorkOrder',
  tag: 'termination',

}
const openWorkOrder = {
  label: '开启',
  buttonTheme: 'light',
  fnName: 'openWorkOrder',
  tag: '',
}
const restartWorkOrder = {
  label: '重启',
  buttonTheme: 'light',
  fnName: 'restartWorkOrder',
  tag: '',

}

Page({
  behaviors: [mixin_dicKey],

  /**
   * 页面的初始数据
   */
  data: {
    // dialog隐藏显示
    visible: false,
    dialogInfo: {
      title: '',
      content: '',
      // 标识
      tag: ''
    },
    // 缓存每次点击值
    itemData: null,
    bodyHeight: 0,
    buttonState: {
      // 待接单
      'wait_accept': [chargebackWorkOrder, acceptOrderWorkOrder],
      // 处理中
      'processing': [completeWorkOrder, handleWorkOrder],
      // 待审核
      'wait_audit': [revokeWorkOrder],
      // 待评价
      'wait_appraisal': [evaluateWorkOrder],
      // 已暂停
      'suspended': [terminationWorkOrder, openWorkOrder, ],
      // 已终止
      'terminated': [restartWorkOrder, ],
      //  审核驳回
      'not_pass_audit': [completeWorkOrder, handleWorkOrder, ]
    },

    sortData: [{
      value: 1,
      label: '综合排序'
    }, {
      value: 2,
      label: '紧急程度优先'
    }, {
      value: 3,
      label: '报修时间优先'
    }, {
      value: 4,
      label: '处理时间优先'
    }, {
      value: 5,
      label: '影响范围优先'
    }],
    sortFilter: [{
      value: '',
      label: '筛选'
    }],
    queryData: {
      // 状态
      currState: '',
      // 紧急程度
      degree: '',
      // 关键字(故障描述/报修电话/报修科室/报修人)
      keywords: '',
      // 影响范围
      rangeType: '',
      // 处理类型
      responseType: '',
      // 排序：1综合;2紧急;3报修时间;4处理时间;5影响范围
      sort: 1
    },
    dropdownItemValue: [],
    dataList: []

  },
  // dialog取消
  onDialogButtonCancel() {
    this.setData({
      visible: false
    })
  },
  // dialog确认
  onDialogButtonConfirm() {
    console.log('onDialogButtonConfirm');
    const self = this
    this.setData({
      visible: false
    })
    const {
      orderNum
    } = this.data.itemData
    // 重启
    if (orderNum && this.data.dialogInfo.tag === 'restartWorkOrder') {
      WorkOrderMyIndexApi.restart(({
        orderNum: orderNum
      }), () => {
        self.getList()
      })
    }

  },
  // 获取工单列表
  getList() {
    const data = this.data.queryData
    const self = this
    WorkOrderMyIndexApi.getlist(data, (res) => {
      console.log(res, '2er');
      self.setData({
        dataList: res.data.rows || []
      })
    })
  },

  // 初始化样式
  styleInit() {
    const self = this
    getElHeightRpx('#header', (height) => {
      self.setData({
        bodyHeight: app.globalData.viewHeight - height
      })
    })
  },
  changeSearch: wx.$_.debounce(function (e) {
    this.setData({
      'queryData.keywords': e.detail.value
    })
    this.getList()
  }, 300),
  // 重置筛选
  onQueryDataReset() {
    this.setData({
      queryData: {
        currState: '',
        degree: '',
        keywords: '',
        rangeType: '',
        responseType: '',
      }
    })
    this.selectComponent('#dropdownItem').closeDropdown();
    this.getList()
  },
  // 筛选
  onQueryDataConfirm(event) {
    this.getList()
  },

  // 点击卡片上的按钮
  onButton(event) {
    console.log(event, 'event');
    const {
      item,
      buttonitem
    } = event.currentTarget.dataset
    this.setData({
      itemData: item
    })
    this[buttonitem.fnName](item, buttonitem.tag)

    console.log(item);
  },

  /**
   * 重启
   */
  restartWorkOrder(data) {
    this.setData({
      itemData: data,
      dialogInfo: {
        title: "重启",
        content: '确认后，工单重启。工单将退回到“待派单”状态，需重新派单',
        tag: 'restartWorkOrder'
      },
      visible: true
    })


  },

  /**
   * 开启
   */
  openWorkOrder() {

  },

  /**
   * 终止
   */
  terminationWorkOrder() {

  },

  /**
   * 评价
   */
  evaluateWorkOrder() {

  },

  /**
   * 撤销
   */
  revokeWorkOrder() {
    this.setData({

      dialogInfo: {
        title: "撤销审核",
        content: '确认后，工单撤回到“处理中”状态，可再次点击“完成”进入审核',
        tag: 'revokeWorkOrder'
      },
      visible: true

    })
  },

  /**
   * 处理
   */
  handleWorkOrder(data, handleState) {
    wx.navigateTo({
      url: `${SUB_PAGES.WorkOrder.WORK_ORDER_MY_INDEX_HANDLE.router}?orderNum=${data.orderNum}&currState=${data.currState}&handleState=${handleState}`,
    })
  },

  /**
   *完成工单 
   * @param {*} data 
   */
  completeWorkOrder() {

  },
  /**
   * 退单
   * @param {*} data 
   */
  chargebackWorkOrder(data) {
    wx.navigateTo({
      url: SUB_PAGES.WorkOrder.WORK_ORDER_MY_INDEX_HANDLE.router,
    })
  },
  /**
   * 接单
   * @param {*} data 
   */
  acceptOrderWorkOrder(data) {
    const self = this
    const {
      orderNum
    } = data
    WorkOrderMyIndexApi.acceptOrder({
      orderNum
    }, ({
      code,
      msg
    }) => {
      if (code === 200) {
        wx.showToast({
          title: '接单成功',
          icon: 'success'
        })
        self.getList()
      } else {
        wx.showToast({
          title: msg,
          icon: 'error'
        })
      }

    })
  },

  // 综合排序
  onChangeSort(e) {
    this.setData({
      'queryData.sort': e.detail.value,
    });
  },
  onTag(e) {
    const key = e.currentTarget.dataset.key;
    const value = e.detail.value;

    this.setData({
      [`queryData.${key}`]: value,
      dropdownItemValue: [1]
    });
  },


  // 监听输入：此方法用于监听用户的输入。
  listenInput() {

  },
  onCard(e) {
    console.log(e);

    wx.navigateTo({
      url: `${SUB_PAGES.WorkOrder.WORK_ORDER_DETAIL.router}${queryParams({
      orderNum:e.detail.value.orderNum,
      orderState: e.detail.value.currState
    })}`
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.styleInit()
    this.getList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})